/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ocumed.presentation.utils;

import java.util.HashMap;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

/**
 *
 * @author apr3455
 */
public class StdComboModel extends AbstractListModel implements ComboBoxModel, SortableModel {
    
    protected HashMap<Integer, String> m_hashMap = null;
    protected Object m_values[] = null;
    protected Object m_keys[] = null;
    protected Integer m_size = null;
    protected String m_selectedItem = null;
    protected Integer m_selectedId = null;

    public StdComboModel(HashMap<Integer, String> hashMap){
        ListSort comboBoxSort = new ListSort();
        m_hashMap = hashMap;
        m_size = m_hashMap.size();
        m_keys = m_hashMap.keySet().toArray();
        m_values = m_hashMap.values().toArray();
        comboBoxSort.sortArray(m_values, this);
    }
    

    public int getSize() {
        return m_size;
    }

    public Object getElementAt(int pos) {
        return m_hashMap.get(m_keys[pos]);
    }
    
    public void setSelectedItem(Object obj) {
        String itemToSelect = (String) obj;
        for(int i = 0; i < m_size; i++){
            if(m_hashMap.get(m_keys[i]).equals(itemToSelect)){
                m_selectedItem = m_hashMap.get(m_keys[i]);
                m_selectedId = (Integer) m_keys[i];
            }
        }
        fireContentsChanged(this, -1, -1);
    }

    public Integer getSelectedId(){
        return m_selectedId;
    }
    
    public Object getSelectedItem() {
        return m_selectedItem;
    }

    public void changeIdPos(int i, int j) {
        Integer temp = (Integer) m_keys[i];
        m_keys[i] =  m_keys[j];
        m_keys[j] = temp;
    }
}
